(async function(){
    if (!checkConnect()) return;
    var rtn = await loadManbers();
    if (rtn){
        //设置会员列表
        setList("members");
        getNode("start").value = new Date().format("yyyy/MM/dd");
        //发放业务
        getNode("sent").onclick=async function(){
            if (!checkConnect()) return;
            //获取输入值
            var member = getList("members");
            var amount = getNode("amount").value;
            var reason = getNode("reason").value;
            var start = getNode("start").value;
            var end = getNode("end").value;
            var lastPay = getNode("lastPay").value;
            var period = getNode("period").selectedIndex;
            //验证输入值
            if (member.length==0) {
                alert("没有选择受益人");
                return;
            }
            if (amount<=0){
                alert("金额必须大于零");
                return;
            }
            if (reason.length==0){
                alert("请输入理由");
                return;
            }
            if (start.length!=10 || start.substr(4,1)!="/" || start.substr(7,1)!="/") {
                alert("开始时间不正确，请按照yyyy/mm/dd格式输入");
                return;
            }
            try{
                start = getUNIXTimestamp(start);
            }catch(e){
                alert("开始时间不正确，请按照yyyy/mm/dd格式输入");
                return;
            }
            if(lastPay){
                if (lastPay.length!=10 || lastPay.substr(4,1)!="/" || lastPay.substr(7,1)!="/") {
                    alert("最后支付时间不正确，请按照yyyy/mm/dd格式输入");
                    return;
                }
                try{
                    lastPay = getUNIXTimestamp(lastPay);
                }catch(e){
                    alert("最后支付时间不正确，请按照yyyy/mm/dd格式输入");
                    return;
                }
            }else lastPay=0;
            //设置周期
            if (period==0) period = ONCE;
            else if (period==1) period = DAY;
            else if (period==2) period = MONTH;
            else if (period==3) period = YEAR;
            if (end * period > 315619200) {
                alert("如果设置，结束时间必须为10年以内");
                return;
            }
            if (end>0) end = start + end * period;
            setLoading(1);
            const rtn = await newSalary("发放薪酬",member[0],reason,start,end,period,amount,lastPay);
            if(rtn){
                //重置输入栏
                clearList("members");
                getNode("reason").value = "";
                getNode("amount").value = 0;
                getNode("start").value = new Date().format("yyyy/MM/dd");
                getNode("end").value = 0;
                getNode("lastPay").value = "";
                getNode("period").selectedIndex = 0;
            }
            setLoading(0);
        };
    }
})();